package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.common.tool.StringUtil;
import com.yike.user.entity.UserConsult;
import com.yike.user.mapper.UserConsultMapper;
import com.yike.user.param.UpdateConsultStatus;
import com.yike.user.param.UserConsultParam;
import com.yike.user.service.UserConsultService;
import com.yike.user.param.UserConsultPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.UserConsultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 用户咨询 服务实现类
 *
 * @author Sakura
 * @since 2025-04-25
 */
@Slf4j
@Service
public class UserConsultServiceImpl extends BaseServiceImpl<UserConsultMapper, UserConsult> implements UserConsultService {

    @Autowired
    private UserConsultMapper userConsultMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUserConsult(UserConsultParam userConsultParam) throws Exception {
        UserConsult userConsult = new UserConsult();
        BeanUtils.copyProperties(userConsultParam, userConsult);
        userConsult.setStatus(0);
        return super.save(userConsult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUserConsult(UserConsult userConsult) throws Exception {
        return super.updateById(userConsult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUserConsult(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public UserConsultVo getUserConsult(Long id) throws Exception {
        UserConsult userConsult = userConsultMapper.selectById(id);
        if (userConsult == null) {
            throw new BusinessException("信息异常");
        }
        UserConsultVo userConsultVo = new UserConsultVo();
        BeanUtils.copyProperties(userConsult, userConsultVo);
        return userConsultVo;
    }

    @Override
    public Paging<UserConsultVo> getUserConsultPageList(UserConsultPageParam userConsultPageParam) throws Exception {
        if (StringUtil.isNotBlank(userConsultPageParam.getBeginTime()) && userConsultPageParam.getBeginTime().length() < 15) {
            userConsultPageParam.setBeginTime(userConsultPageParam.getBeginTime() + " 00:00:00");
        }
        if (StringUtil.isNotBlank(userConsultPageParam.getEndTime()) && userConsultPageParam.getEndTime().length() < 15) {
            userConsultPageParam.setEndTime(userConsultPageParam.getEndTime() + " 23:59:59");
        }
        Page<UserConsult> page = new PageInfo<>(userConsultPageParam);
        IPage<UserConsultVo> iPage = userConsultMapper.getUserConsultList(page, userConsultPageParam);
        return new Paging<UserConsultVo>(iPage);
    }

    @Override
    public boolean updateStatus(UpdateConsultStatus updateConsultStatus) throws Exception {
        UserConsult userConsult = userConsultMapper.selectById(updateConsultStatus.getId());
        if (userConsult == null) {
            throw new BusinessException("信息异常");
        }
        userConsult.setStatus(updateConsultStatus.getStatus());
        userConsult.setUpdateTime(new Date());
        userConsult.setRemark(updateConsultStatus.getRemark());
        userConsultMapper.updateById(userConsult);

        return true;
    }

    @Override
    public Integer getUnreadNum() throws Exception {
        return userConsultMapper.getUnreadNum();
    }

}
